package flc.ast.fragment;

import android.content.Context;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.os.StatFs;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.ScreenUtils;

import java.util.Random;

import flc.ast.R;
import flc.ast.databinding.FragmentHomeTabBinding;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.device.BatteryUtil;
import stark.common.basic.device.DeviceInfoUtil;
import stark.common.basic.device.StorageUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeTabFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeTabFragment extends BaseNoModelFragment<FragmentHomeTabBinding> {


    @Override
    protected int onCreate() {
        return R.layout.fragment_home_tab;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        mDataBinding.tvModel.setText(DeviceInfoUtil.getDeviceDevice());
        mDataBinding.tvAndroid.setText(android.os.Build.VERSION.RELEASE);

        StatFs statFs = StorageUtil.getInternalMemory();//内部存储
        //已使用
        long usedLong = statFs.getTotalBytes() - statFs.getAvailableBytes();
        //总内存
        long allLong = statFs.getTotalBytes();
        //已用百分比
        int progress = Math.round((statFs.getTotalBytes() - statFs.getAvailableBytes()) * 100f / statFs.getTotalBytes());
        mDataBinding.tvStorage.setText(ConvertUtils.byte2FitMemorySize(usedLong, 0) + "/" + ConvertUtils.byte2FitMemorySize(allLong, 0));
        mDataBinding.pbProgress.setProgress(progress);

        mDataBinding.tvOption1.setText(getRandomNum() + "");
        mDataBinding.tvOption2.setText(getRandomNum() + "");
        mDataBinding.tvOption3.setText(getRandomNum() + "");
        mDataBinding.tvOption4.setText(getRandomNum() + "");
        mDataBinding.tvOption5.setText(getRandomNum() + "");
        mDataBinding.tvOption6.setText(getRandomNum() + "");
        mDataBinding.tvOption7.setText(getRandomNum() + "");
        mDataBinding.tvOption8.setText(getRandomNum() + "");

        BatteryUtil.BatteryInfo batteryInfo = BatteryUtil.getBatteryInfo(mContext);
        int level = batteryInfo.getLevel();
        mDataBinding.pbBattery.setProgress(level);
        int temperature = 20 + new Random().nextInt(10);
        mDataBinding.tvBattery.setText(getString(R.string.current_battery) + level + getString(R.string.unit_percent));
        mDataBinding.tvBatteryTotal.setText(getString(R.string.temperature_text) + temperature + getString(R.string.unit_temperature));

        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        mDataBinding.tvScreen.setText(ScreenUtils.getScreenWidth() + " x " + ScreenUtils.getScreenHeight() + "  " + (int) display.getRefreshRate() + "Hz");
    }

    private int getRandomNum() {
        return 1000 + new Random().nextInt(500);
    }

}